/*
 @description: 图片上传

 @author: 李建翰

 @update: 2021年9月4日17:06:36
*/
'use strict';
const Controller = require('egg').Controller;
const fs = require('fs');
const path = require('path');
class PictureController extends Controller {
    async uploadImg(){
        const {ctx,app} = this;
        const file = ctx.request.files[0];
        
        if(file){
            file.filename = Math.round(Math.random()*100+1)+Date.now()+path.extname(file.filename);
            const pathName = 'app/public/img/'+file.filename;
            const path1='public/img/'+file.filename;
            // const rs = fs.createReadStream(file.filepath);
            // const ws = fs.createWriteStream(pathName);
            // rs.pipe(ws);
            fs.writeFileSync(pathName,fs.readFileSync(file.filepath))
            const data = await app.mysql.insert('picture',{
                upl:pathName
            })

            ctx.body = {
                code:0,
                msg:'成功上传图片',
                data:path1
            }
        }else{
            ctx.body = {
                code:0,
                msg:'上传图片失败'
            }
        }
    }
    async show(){
        const {ctx,app} = this;
        const {id} = ctx.params;
        const result=await app.mysql.get('picture',{ID:id})
        if(result){
            ctx.body={
                code:0,
                msg:'查找成功',
                data:result
            };
        }else{
            ctx.body = {
                code:4007,
                msg:"查找失败"
            }
        }
    }
}

module.exports = PictureController;